import apiClient from './http';

export default {
  // 新增商品及详情
  saveCommodity(commodity, detail) {
    const formData = new FormData();
    // 处理商品主数据
    Object.keys(commodity).forEach(key => {
      formData.append(key, commodity[key]);
    });
    // 处理详情数据（如果存在）
    if (detail) {
      Object.keys(detail).forEach(key => {
        formData.append(`detail.${key}`, detail[key]);
      });
    }
    return apiClient.post('/data/commodity/save', formData);
  },

  // 更新商品
  updateCommodity(commodity, detail) {
    const formData = new FormData();
    // 处理商品主数据
    Object.keys(commodity).forEach(key => {
      formData.append(key, commodity[key]);
    });
    // 处理详情数据（如果存在）
    if (detail) {
      Object.keys(detail).forEach(key => {
        formData.append(`detail.${key}`, detail[key]);
      });
    }
    return apiClient.put('/data/commodity/update', formData);
  },

  // 更新商品详情
  updateCommodityDetail(detail) {
    return apiClient.put('/data/commodity/updateDetail', detail);
  },

  // 删除商品及详情
  deleteCommodity(id) {
    return apiClient.delete(`/data/commodity/${id}`);
  },

  // 分页查询商品
  getCommodityPage(keyword, pageNum, pageSize) {
    return apiClient.get('/data/commodity/page', {
      params: { keyword, pageNum, pageSize }
    });
  },

  // 全文搜索商品
  searchCommodity(keyword) {
    return apiClient.get('/data/commodity/search', {
      params: { keyword }
    });
  },

  // 获取商品详情
  getCommodityDetail(id) {
    return apiClient.get(`/data/commodity/detail/${id}`);
  }
};
